package net.sxnic.item.print.word;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringUtils;


import net.sxnic.comm.basecode.BaseCodeUtils;
import net.sxnic.comm.utils.WordUtils;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.jhitem.itemannex.ItemAnnex;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemssmb.ItemSsmb;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImportFormatMode;

public class rkxInfo {
	
	static {
		Document.setLicence("sj_laokai");
	}
	//实施目标索引
	private static int SSMBROWINDEX = 12;
	private static int TABLEINDEXSSMB = 1;
	private static int SSMBROWMAX = 5;
	 //研究人员索引
	private static int PEOPLEROWINDEX =33;
	private static int TABLEINDEXPEOPLE =2;
	private static int PEOPLEROWMAX = 5;

	public static void createWord(String saveDocFile, String templateFile,
			Mdata mdata,ItemFund fund,ItemPrincipal principal,ItemClob clob,List<ItemPeople> peoples,List<ItemSsmb> ssmbs, List<ItemAnnex> annex002List,String basePath) {
		Document doc;
		try {
			File file = new File(saveDocFile);
			if (!file.getParentFile().exists()
					&& !file.getParentFile().mkdirs()) {
				return;
			}
			doc = new Document(templateFile);
			DocumentBuilder builder = new DocumentBuilder(doc);
			SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			
			
			// 设置标签
			WordUtils.setBookmarks(doc, mdata);
			WordUtils.setBookmarks(doc, clob);
			WordUtils.setBookmarks(doc, fund);
			WordUtils.setBookmarks(doc,principal);
			
			//设置项目类别
			WordUtils.setBookmark(doc, "unitType",BaseCodeUtils.getInfoName("012", mdata.getUnitType()));
			WordUtils.setBookmark(doc, "sex",
					BaseCodeUtils.getInfoName("002", principal.getSex()));
			WordUtils.setBookmark(doc, "techTitle",
					BaseCodeUtils.getInfoName("024", principal.getTechTitle()));
			WordUtils.setBookmark(doc, "highestOffer",
					BaseCodeUtils.getInfoName("007", principal.getHighestOffer()));
			if(StringUtils.isNotBlank(mdata.getZzdwCode())){
			WordUtils.setBookmark(doc,"zzdwCode",ItemConstants.ZZDW_MAP.get(ItemConstants.SYSTEM_CALALOG_ALIAS).get(mdata.getZzdwCode()));
			}
			//项目分阶段实施目标
			if (ssmbs != null && ssmbs.size() > 0) {
				builder.moveToBookmark("ssmb_table");

				int tableIndex = TABLEINDEXSSMB;
				int j = SSMBROWINDEX;// 开始表格Row, 这里的index从0开始


				for (ItemSsmb ssmb : ssmbs) {
					// 实施时间区间
					builder.moveToCell(tableIndex, j, 0, 0);
					builder.write(ssmb.getStime()+"至"+ssmb.getEtime());
					// 实施内容
					builder.moveToCell(tableIndex, j, 1, 0);
					builder.write(ssmb.getContent());
					j++;
					if ((j - SSMBROWINDEX) >= SSMBROWMAX) {
						break;
					}
				}
			}
			// 主要研究人员
						if(null!=peoples&&peoples.size()>0){
							
							builder.moveToBookmark("designer");
							int j=PEOPLEROWINDEX;
							
							for(ItemPeople p : peoples){
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 0, 0);  
							   builder.write(p.getChinName());
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 1, 0);
							   builder.write(BaseCodeUtils.getInfoName("002", p.getSex()));
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 2, 0);
							   builder.write(p.getBorn());
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 3, 0);
							   builder.write(BaseCodeUtils.getInfoName("006", p.getEducate()));
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 4, 0);
							   builder.write(BaseCodeUtils.getInfoName("024", p.getScienceTitle()));
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 5, 0);
							   builder.write(p.getSpecialty());
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 6, 0);
							   builder.write(p.getTask());
							   builder.moveToCell(TABLEINDEXPEOPLE, j, 7, 0);
							   builder.write(p.getUnit());
							   
							   j++;
							   
							   if ((j - PEOPLEROWINDEX) >= PEOPLEROWMAX) {
									break;
								}
							}
						}
						//文档合并
						if(annex002List!=null&&annex002List.size()>0)
						{
							for(ItemAnnex itemAnnex : annex002List)
							{
								doc.appendDocument(new Document(itemAnnex.getFilePath()+itemAnnex.getAttachName()), ImportFormatMode.KEEP_SOURCE_FORMATTING);
							}

						}
			doc.save(saveDocFile);
			
		} catch (Exception ex) {
			ex.printStackTrace();
			try {
				doc = new Document(templateFile);
				doc.save(saveDocFile);
			} catch (Exception e) {
				e.fillInStackTrace();
			}
		}
	}
	
}
